package com.springboot.service.impl;

import com.springboot.mapper.DoctorAppointSetMapper;
import com.springboot.mapper.DoctorInfoFormMapper;
import com.springboot.model.DoctorAppointSet;
import com.springboot.model.DoctorInfoForm;
import com.springboot.service.DoctorAppointSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;

/**
 * 医生设置预约时间
 */
@Service
public class DoctorAppointSetImpl implements DoctorAppointSetService {

    @Autowired
    private DoctorInfoFormMapper doctorInfoFormMapper;

    @Autowired
    private DoctorAppointSetMapper doctorAppointSetMapper;

    //通过手机号码查找医生
    @Override
    public DoctorInfoForm findDoctorByTel(String tel)
    {
        DoctorInfoForm doctorInfoForm= new DoctorInfoForm();
        try{
            doctorInfoForm=doctorInfoFormMapper.selectByTel(tel) ;
        }catch (Exception e)
        {
            System.out.println("DoctorAppointSet业务无法通过手机号查找到医生信息");
        }
        return doctorInfoForm;
    }

    //通过预约id查找医生预约列表
    @Override
    public  ArrayList<DoctorAppointSet>  findDoctorAppointById(Integer id)
    {
        ArrayList<DoctorAppointSet>  doctorAppointSets= new ArrayList<>();
        try{
            doctorAppointSets=doctorAppointSetMapper.selectByDoctorId(id);
        }catch (Exception e)
        {
            System.out.println("DoctorAppointSet无法通过医生id查找到预约时间记录");
        }
        return doctorAppointSets;
    }

    //更新医生设置可预约时间表
    @Override
    public int updateDoctorSet(DoctorAppointSet doctorAppoint)
    {
        return doctorAppointSetMapper.updateByPrimaryKey(doctorAppoint) ;
    }

    //向医生设置可预约时间表中插入数据
    @Override
    public int insertDoctorAppoint(DoctorAppointSet doctorAppoint)
    {
        return doctorAppointSetMapper.insert(doctorAppoint) ;
    }

    @Override
    public ArrayList<DoctorAppointSet> selectAllDoctorOrder() {
        ArrayList<DoctorAppointSet> doctorsAppointSets = doctorAppointSetMapper.selectAllDoctorOrder();
        doctorsAppointSets.sort(new Comparator<DoctorAppointSet>() {
            @Override
            public int compare(DoctorAppointSet o1, DoctorAppointSet o2) {
                return o1.getBookDate().compareTo(o2.getBookDate());
            }
        });
        return doctorsAppointSets;
    }

}
